package com.chinacoal.hr.levelimpl.dao;

import com.baomidou.mybatisplus.core.conditions.query.Query;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chinacoal.hr.levelapi.entity.FileInfo;
import com.chinacoal.hr.levelimpl.mapper.FileInfoMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-12-04 22:24
 * @description:
 **/
@Component
public class FileInfoDao {

    @Resource
    public FileInfoMapper fileInfoMapper;

    public void insertFileInfo(FileInfo record) throws Throwable {
        this.fileInfoMapper.insert(record);
    }

    /**
     * 按条件查询出不带分页的文件信息列表
     *
     * @param condition :
     * @return : java.util.List<com.chinacoal.hr.levelimpl.entity.FileInfo>
     * @author : YunTao.Li
     * @date : 2019/12/4 2019/12/4
     */
    public List<FileInfo> getFileInfoListWithOutPage(Map<String, Object> condition) throws Throwable {
        QueryWrapper queryWrapper = new QueryWrapper();

        String paDocId = condition.get("paDocId") != null ? condition.get("paDocId").toString() : "";

        if (StringUtils.isNotBlank(paDocId)) {
            queryWrapper.eq("pa_doc_id", paDocId);
        }
        queryWrapper.orderByDesc("CREATION_DATE");

        List<FileInfo> fileInfos = this.fileInfoMapper.selectList(queryWrapper);

        return fileInfos;
    }


    public void updateFileInfo(FileInfo template, FileInfo value) {
        String tempPaDocId = template.getPaDocId();

        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(tempPaDocId)) {
            queryWrapper.eq("pa_doc_id", tempPaDocId);
        }


        this.fileInfoMapper.update(value, queryWrapper);
    }

    /**
     * 获得文件对象
     *
     * @param condition :
     * @return : com.chinacoal.hr.levelimpl.entity.FileInfo
     * @author : YunTao.Li
     * @date : 2020/1/2 2020/1/2
     */
    public FileInfo getFileInfo(Map<String, Object> condition) throws Throwable {
        String fileId = condition.get("fileId") != null ? condition.get("fileId").toString() : "";
        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(fileId)) {
            queryWrapper.eq("FILE_ID", fileId);
        }
        queryWrapper.last("limit 1");

        return this.fileInfoMapper.selectOne(queryWrapper);
    }

    /**
     * 删除文件
     *
     * @param fileId :
     * @return : void
     * @author : YunTao.Li
     * @date : 2020/1/2 2020/1/2
     */
    public void deleteBizTravelById(String fileId) {
        this.fileInfoMapper.deleteById(fileId);
    }

}
